package com.iflytek.cosmo.ocr.common.core.worker;

import java.io.ByteArrayOutputStream;

import com.iflytek.cosmo.ocr.common.core.BeanHelper;
import com.iflytek.cosmo.ocr.common.core.station.AbstractStation;
import com.iflytek.cosmo.ocr.common.core.task.RecognizedTask;
import com.iflytek.cosmo.ocr.model.domain.FileInfo;
import com.iflytek.cosmo.ocr.service.FileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *图片压缩
 */
public class CompressWorker implements Runnable {

    /**
     * 日志记录器
     */
    private Logger logger = LoggerFactory.getLogger(CompressWorker.class);

    private RecognizedTask task;
    /**
     * 下一个工作台
     */
    private AbstractStation nextStation;

    public CompressWorker(RecognizedTask task, AbstractStation nextStation) {
        this.task = task;
        this.nextStation = nextStation;
    }


    @Override
    public void run() {
        logger.debug("开始图片上传压缩任务, parentFileName: {}", task.isPdf() ? task.getParentFileInfo().getFileName() : "NULL");

        FileService fileService= BeanHelper.getFileServiceBean();

        RecognizedTask rTask = new RecognizedTask();

        FileInfo parentFileInfo = task.getParentFileInfo();
        if (task.isPdf()) {
            ByteArrayOutputStream outputStream = (ByteArrayOutputStream) task.getImageOutputStream();
            int index = task.getIndex();
            try {
                //判断父pdf是否存在 如果父文件被删除则不往里面插入
                FileInfo fileInfoParent = fileService.selectFileInfo(parentFileInfo.getId());
                if (fileInfoParent.getId() == null) {
                    logger.debug("-------父文件删除不往里面插入-------" + parentFileInfo.getId());
                    return;
                }

                // 子文件信息上传到fdfs
                String fileName = parentFileInfo.getFileName() + "_" + index;
                String fileType = "png";
                byte[] bytes = outputStream.toByteArray();
                long fileSize = bytes.length;
                String filePath = fileService.uploadFile(fileName, fileType, bytes);
                outputStream.close();

                // 保存子文件信息
                FileInfo fileInfo = new FileInfo();
                fileInfo.setDirId(parentFileInfo.getDirId());
                fileInfo.setUserId(parentFileInfo.getUserId());
                fileInfo.setFileName(fileName);
                fileInfo.setFileType(fileType);
                fileInfo.setFileSize(fileSize);
                fileInfo.setFilePath(filePath);
                // 子文件添加的属性
                fileInfo.setParentId(parentFileInfo.getId());
                fileInfo.setFilePage(index);

                String fileId = fileService.saveFileInfo(fileInfo);
                fileInfo.setId(fileId);
                rTask.setFileInfo(fileInfo);

                // 如果父文件有多页则等待用户选择要识别的页码, 单页则直接识别
                if (parentFileInfo.getFilePage().equals(1)) {
                    // pdf文件借用formatSize保存实际要识别的页数, 为了后续查询识别进度
                    fileInfoParent.setFormatSize(1L);
                    fileService.saveFileInfo(fileInfoParent);

                    this.nextStation.add(rTask);
                }
            }
            catch (Exception e) {
                logger.warn("pdf页面上传压缩任务异常, pdfName: {}, index: {}", parentFileInfo.getFileName(), task.getIndex());
            }
        }
        else {
            //非pdf文件
            RecognizedTask rTaskPic = task;
            this.nextStation.add(rTaskPic);
        }
        return;
    }
}
